﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace RoadTeam.FineFile
{
    public class FileOpera
    {
        public String SourceDir { get; set; }
        public String OutDir { get; set; }
        public String FileDirAndName { get; set; }


        public void CopyFile()
        {
            CopyFile(SourceDir, OutDir, FileDirAndName);
        }

        public void CopyFile(string SourceDir, string OutDir, string FileDirAndName)
        {
            //把路径标准化,把/替换为\
            FileDirAndName = FileDirAndName.Replace("/", "\\");

            String Temp = SourceDir + "\\" + FileDirAndName;
            //如果指定的文件不存在,则不创建该目录
            if (!File.Exists(Temp)) return;

            List<string> DirNameArr = FileDirAndName.Split('\\').ToList();
            if (DirNameArr != null || DirNameArr.Count > 0)
            {
                for (int i = 0; i < DirNameArr.Count; i++)
                {
                    string Item = DirNameArr[i];
                    if (!string.IsNullOrEmpty(Item))
                    {
                        //判断是不是最后一个文件名
                        if (i == (DirNameArr.Count - 1))
                        {   //正式复制文件
                            CopyFileToTarget(SourceDir, OutDir, Item);
                        }
                        //如果是文件夹
                        else if (!Directory.Exists(OutDir + "\\" + Item) && Directory.Exists(SourceDir + "\\" + Item))
                        {
                            Directory.CreateDirectory(OutDir + "\\" + Item);
                        }
                        SourceDir += "\\" + Item;
                        OutDir += "\\" + Item;
                    }
                }
            }
        }

        private void CopyFileToTarget(String source, String destination, string CopyFileName)
        {
            try
            {
                DirectoryInfo info = new DirectoryInfo(source);
                FileSystemInfo[] sysInfos = info.GetFileSystemInfos();

                foreach (FileSystemInfo fsi in info.GetFileSystemInfos())
                {
                    string destName = Path.Combine(destination, fsi.Name);
                    if (fsi is System.IO.FileInfo)
                    {
                        string sourceName = Path.Combine(source, fsi.Name);
                        try
                        {
                            //just in case the destination is readonly
                            if (File.Exists(destName))
                            {
                                new FileInfo(destName).IsReadOnly = false;
                            }
                            if (fsi.Name == CopyFileName)
                            {
                                File.Copy(fsi.FullName, destName, true);
                            }
                        }
                        catch (Exception ex)
                        {
                            throw new Exception("出错了,错误信息:" + ex.Message);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception("出错了,错误信息:" + ex.Message);
            }
        }


        public static String GetFileName(String StrPath)
        {
            StrPath = StrPath.Replace('/', '\\');
            int index = StrPath.LastIndexOf('\\');
            return StrPath.Substring(index + 1, StrPath.Length - index - 1);
        }
    }
}
